package com.jumi.microservice.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jumi.microservice.domain.GoodsWarehouseStockDO;
import com.jumi.microservice.domain.OrderWarehouseInfoDTO;
import com.jumi.microservice.mapper.GoodsWarehouseStockMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * - - - - - - - - - - - - - - -
 * 订单-库存服务接口实现 feign
 * @author liuz
 * @date 2020/9/22 13:37
 * ToDoSomeThing
 * - - - - - - - - - - - - - - -
 */
@Service
public class OrderInventoryService implements OrderInventoryApi {

    @Autowired
    GoodsWarehouseStockMapper goodsWarehouseStockMapper;


    /**
     * 订单获取sku仓库库存信息
     * @param warehouseInfoDTOS sku仓库库存信息
     * @return
     */
    @Override
    public List<OrderWarehouseInfoDTO> wareHouseInfo4Order(List<OrderWarehouseInfoDTO> warehouseInfoDTOS) {
        try {
            List<Long> skuids = new ArrayList<>();
            //取出skuid的集合
            warehouseInfoDTOS.stream().forEach(dto -> {
                skuids.add(dto.getSkuId());
            });
            if(CollectionUtil.isNotEmpty(skuids)) {
                List<OrderWarehouseInfoDTO> warehouseInfos = new ArrayList<>();
                List<GoodsWarehouseStockDO> warehouseStockDOS = goodsWarehouseStockMapper.selectList(
                        Wrappers.<GoodsWarehouseStockDO>lambdaQuery()
                                .in(GoodsWarehouseStockDO::getGoodsSkuId, skuids));
                //封装sku的仓库信息
                if (CollectionUtil.isNotEmpty(warehouseStockDOS)) {
                    warehouseStockDOS.stream().forEach(po -> {
                        OrderWarehouseInfoDTO warehouseStockDO = new OrderWarehouseInfoDTO();
                        warehouseStockDO.setWarehouseName(po.getWarehouseName());
                        warehouseStockDO.setSaleStockQuantity(po.getSaleStockQuantity());
                        warehouseStockDO.setSkuId(po.getGoodsSkuId());
                        warehouseStockDO.setWarehouseId(po.getWarehouseId());
                        warehouseInfos.add(warehouseStockDO);
                    });
                }
                return warehouseInfos;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return warehouseInfoDTOS;
    }
}
